package com.shengyo.model.dao.impl;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.shengyo.model.bean.Brand;
import com.shengyo.model.dao.IBrandDAO;

public class BrandDAOImpl extends BaseDAOImpl implements IBrandDAO{

	public void delete(Brand brand) {
		this.getHibernateTemplate().delete(brand);
		
	}

	@SuppressWarnings("unchecked")
	public List<Brand> findAll() {
		return this.getHibernateTemplate().find("from brand");
	}

	@SuppressWarnings("unchecked")
	public List<Brand> findAll(final int first, final int max) {
		
		return this.getHibernateTemplate().executeFind(new HibernateCallback(){
			
			public Object doInHibernate(Session session)
					throws HibernateException,SQLException{
				return session.createQuery("from brand")
							  .setFirstResult(first)
							  .setMaxResults(max)
							  .list();
			}
		});
	}

	
	public Object findById(int id) {
		return null;
	}

	@SuppressWarnings("unchecked")
	public Brand findByName(String name) {
		List list = this.getHibernateTemplate().find("from brand b where b.name=?",new Object[]{name});
		if (list != null && !list.isEmpty())
			return (Brand) list.get(0);
		else
			return null;
	}

	@Override
	public Brand findById(String id) {
		return (Brand) this.getHibernateTemplate().load(Brand.class, id);
	}

}
